package com.ex.mzwqz.activity;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.ex.mzwqz.R;
import com.ex.mzwqz.model.UserEntity;
import com.ex.mzwqz.util.Constants;
import com.ex.mzwqz.util.SharePrefenceUtils;
import com.ex.mzwqz.util.UiManager;

/**
 * Created by xiaguoqiang on 16/10/19.
 */
public class LoginActivity extends BaseActivity{

    private View loginPage;
    private View splashPage;
    private boolean autoLogin = false;
    private EditText mNameView;
    private EditText mPasswordView;
    private InputMethodManager intputManager;


    @Override
    protected int setContentViewResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        splashPage = findViewById(R.id.splash_page);
        loginPage = findViewById(R.id.login_page);
        mNameView = (EditText) findViewById(R.id.name);
        mPasswordView = (EditText) findViewById(R.id.password);

        findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserEntity user = new UserEntity();
                user.setId("aaasdasdas");
                SharePrefenceUtils.saveUser(LoginActivity.this,user);
                UiManager.toMainView(LoginActivity.this, Constants.SYS_USER_TYPE_MZW);
            }
        });

        findViewById(R.id.sign_in_button).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                UserEntity user = new UserEntity();
                user.setId("aaasdasdas");
                SharePrefenceUtils.saveUser(LoginActivity.this,user);
                UiManager.toMainView(LoginActivity.this, Constants.SYS_USER_TYPE_QY);
                return false;
            }
        });

    }

    @Override
    protected void initData() {
        intputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
        initAutoLogin();
    }

    @Override
    protected void initEvent() {

    }

    private void initAutoLogin() {
        autoLogin = (SharePrefenceUtils.getUser(this) != null);
        splashPage.setVisibility(autoLogin ? View.VISIBLE : View.GONE);
        loginPage.setVisibility(autoLogin ? View.GONE : View.VISIBLE);
        loginPage.setOnTouchListener(new View.OnTouchListener() {


            public boolean onTouch(View v, MotionEvent event) {

                if (mPasswordView != null) {
                    intputManager.hideSoftInputFromWindow(mPasswordView.getWindowToken(), 0);
                }

                if (mNameView != null) {
                    intputManager.hideSoftInputFromWindow(mNameView.getWindowToken(), 0);
                }

                return false;
            }
        });

        if (autoLogin) {
            Animation splashAnimation = AnimationUtils.loadAnimation(this, R.anim.login_splash);
//            mNameView.setText(AppContext.get().userEntity.getName());
            splashAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    UiManager.toMainView(LoginActivity.this,Constants.SYS_USER_TYPE_MZW);
                    LoginActivity.this.finish();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
//            mPasswordView.setText(AppContext.get().userEntity.getPassword());
            if (splashAnimation == null) {
                return;
            }
            splashPage.startAnimation(splashAnimation);
        }
    }

    private void showLoginPage() {
        splashPage.setVisibility(View.GONE);
        loginPage.setVisibility(View.VISIBLE);
    }


}
